{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: tensorflow in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (2.11.0)"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.\n",
      "You should consider upgrading via the 'C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Requirement already satisfied: tensorflow-intel==2.11.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow) (2.11.0)\n",
      "Requirement already satisfied: keras<2.12,>=2.11.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.0)\n",
      "Requirement already satisfied: typing-extensions>=3.6.6 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (4.4.0)\n",
      "Requirement already satisfied: protobuf<3.20,>=3.9.2 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.19.6)\n",
      "Requirement already satisfied: wrapt>=1.11.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.14.1)\n",
      "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.28.0)\n",
      "Requirement already satisfied: opt-einsum>=2.3.2 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.3.0)\n",
      "Requirement already satisfied: six>=1.12.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.16.0)\n",
      "Requirement already satisfied: termcolor>=1.1.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.1.1)\n",
      "Requirement already satisfied: h5py>=2.9.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.7.0)\n",
      "Requirement already satisfied: packaging in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (22.0)\n",
      "Requirement already satisfied: grpcio<2.0,>=1.24.3 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.51.1)\n",
      "Requirement already satisfied: tensorflow-estimator<2.12,>=2.11.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.0)\n",
      "Requirement already satisfied: astunparse>=1.6.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.6.3)\n",
      "Requirement already satisfied: libclang>=13.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (14.0.6)\n",
      "Requirement already satisfied: flatbuffers>=2.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (22.12.6)\n",
      "Requirement already satisfied: google-pasta>=0.1.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.2.0)\n",
      "Requirement already satisfied: absl-py>=1.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.3.0)\n",
      "Requirement already satisfied: numpy>=1.20 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.23.5)\n",
      "Requirement already satisfied: gast<=0.4.0,>=0.2.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.4.0)\n",
      "Requirement already satisfied: tensorboard<2.12,>=2.11 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.0)\n",
      "Requirement already satisfied: setuptools in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (60.2.0)\n",
      "Requirement already satisfied: wheel<1.0,>=0.23.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from astunparse>=1.6.0->tensorflow-intel==2.11.0->tensorflow) (0.37.1)\n",
      "Requirement already satisfied: werkzeug>=1.0.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.2.2)\n",
      "Requirement already satisfied: google-auth<3,>=1.6.3 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.15.0)\n",
      "Requirement already satisfied: requests<3,>=2.21.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.28.1)\n",
      "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.6.1)\n",
      "Requirement already satisfied: markdown>=2.6.8 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.4.1)\n",
      "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.8.1)\n",
      "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.4.6)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (4.9)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.2.8)\n",
      "Requirement already satisfied: cachetools<6.0,>=2.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (5.2.0)\n",
      "Requirement already satisfied: requests-oauthlib>=0.7.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.3.1)\n",
      "Requirement already satisfied: importlib-metadata>=4.4 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from markdown>=2.6.8->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (5.1.0)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.26.13)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.4)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2022.12.7)\n",
      "Requirement already satisfied: charset-normalizer<3,>=2 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.1.1)\n",
      "Requirement already satisfied: MarkupSafe>=2.1.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from werkzeug>=1.0.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.1.1)\n",
      "Requirement already satisfied: zipp>=0.5 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.11.0)\n",
      "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.4.8)\n",
      "Requirement already satisfied: oauthlib>=3.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.2.2)\n"
     ]
    }
   ],
   "source": [
    "!pip install tensorflow"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pandas in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (1.5.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from pandas) (2022.6)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: numpy>=1.20.3 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from pandas) (1.23.5)\n",
      "Requirement already satisfied: six>=1.5 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.\n",
      "You should consider upgrading via the 'C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install pandas"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: neural_structured_learning in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (1.4.0)\n",
      "Requirement already satisfied: six in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from neural_structured_learning) (1.16.0)\n",
      "Requirement already satisfied: attrs in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from neural_structured_learning) (22.1.0)\n",
      "Requirement already satisfied: absl-py in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from neural_structured_learning) (1.3.0)\n",
      "Requirement already satisfied: scipy in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from neural_structured_learning) (1.9.3)\n",
      "Requirement already satisfied: numpy<1.26.0,>=1.18.5 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scipy->neural_structured_learning) (1.23.5)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.\n",
      "You should consider upgrading via the 'C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install neural_structured_learning"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: scikit_learn in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (1.2.0)\n",
      "Requirement already satisfied: joblib>=1.1.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scikit_learn) (1.2.0)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scikit_learn) (3.1.0)\n",
      "Requirement already satisfied: scipy>=1.3.2 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scikit_learn) (1.9.3)\n",
      "Requirement already satisfied: numpy>=1.17.3 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scikit_learn) (1.23.5)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.\n",
      "You should consider upgrading via the 'C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install scikit_learn"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "executionInfo": {
     "elapsed": 4460,
     "status": "ok",
     "timestamp": 1670604423667,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "bTL3Ufo0t487",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import pandas as pd\n",
    "import neural_structured_learning as nsl\n",
    "import numpy as np\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn import preprocessing\n",
    "from sklearn.model_selection import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "executionInfo": {
     "elapsed": 1214,
     "status": "ok",
     "timestamp": 1670604426852,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "xdB9GixktNz0",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv('CSE_CIC_IDS_2018/02-14-2018.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 300
    },
    "executionInfo": {
     "elapsed": 14,
     "status": "ok",
     "timestamp": 1670604426856,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "856UWFEmyUms",
    "outputId": "a5a2bcaf-4a37-454e-eb77-2ffe6b9b956f",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   Dst Port  Protocol            Timestamp  Flow Duration  Tot Fwd Pkts  \\\n0         0         0  14/02/2018 08:31:01      112641719             3   \n1         0         0  14/02/2018 08:33:50      112641466             3   \n2         0         0  14/02/2018 08:36:39      112638623             3   \n3        22         6  14/02/2018 08:40:13        6453966            15   \n4        22         6  14/02/2018 08:40:23        8804066            14   \n\n   Tot Bwd Pkts  TotLen Fwd Pkts  TotLen Bwd Pkts  Fwd Pkt Len Max  \\\n0             0                0                0                0   \n1             0                0                0                0   \n2             0                0                0                0   \n3            10             1239             2273              744   \n4            11             1143             2209              744   \n\n   Fwd Pkt Len Min  ...  Fwd Seg Size Min  Active Mean  Active Std  \\\n0                0  ...                 0          0.0         0.0   \n1                0  ...                 0          0.0         0.0   \n2                0  ...                 0          0.0         0.0   \n3                0  ...                32          0.0         0.0   \n4                0  ...                32          0.0         0.0   \n\n   Active Max  Active Min   Idle Mean    Idle Std  Idle Max  Idle Min   Label  \n0           0           0  56320859.5  139.300036  56320958  56320761  Benign  \n1           0           0  56320733.0  114.551299  56320814  56320652  Benign  \n2           0           0  56319311.5  301.934596  56319525  56319098  Benign  \n3           0           0         0.0    0.000000         0         0  Benign  \n4           0           0         0.0    0.000000         0         0  Benign  \n\n[5 rows x 80 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Dst Port</th>\n      <th>Protocol</th>\n      <th>Timestamp</th>\n      <th>Flow Duration</th>\n      <th>Tot Fwd Pkts</th>\n      <th>Tot Bwd Pkts</th>\n      <th>TotLen Fwd Pkts</th>\n      <th>TotLen Bwd Pkts</th>\n      <th>Fwd Pkt Len Max</th>\n      <th>Fwd Pkt Len Min</th>\n      <th>...</th>\n      <th>Fwd Seg Size Min</th>\n      <th>Active Mean</th>\n      <th>Active Std</th>\n      <th>Active Max</th>\n      <th>Active Min</th>\n      <th>Idle Mean</th>\n      <th>Idle Std</th>\n      <th>Idle Max</th>\n      <th>Idle Min</th>\n      <th>Label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>0</td>\n      <td>14/02/2018 08:31:01</td>\n      <td>112641719</td>\n      <td>3</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>56320859.5</td>\n      <td>139.300036</td>\n      <td>56320958</td>\n      <td>56320761</td>\n      <td>Benign</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>0</td>\n      <td>14/02/2018 08:33:50</td>\n      <td>112641466</td>\n      <td>3</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>56320733.0</td>\n      <td>114.551299</td>\n      <td>56320814</td>\n      <td>56320652</td>\n      <td>Benign</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>0</td>\n      <td>14/02/2018 08:36:39</td>\n      <td>112638623</td>\n      <td>3</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>56319311.5</td>\n      <td>301.934596</td>\n      <td>56319525</td>\n      <td>56319098</td>\n      <td>Benign</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>22</td>\n      <td>6</td>\n      <td>14/02/2018 08:40:13</td>\n      <td>6453966</td>\n      <td>15</td>\n      <td>10</td>\n      <td>1239</td>\n      <td>2273</td>\n      <td>744</td>\n      <td>0</td>\n      <td>...</td>\n      <td>32</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.000000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>Benign</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>22</td>\n      <td>6</td>\n      <td>14/02/2018 08:40:23</td>\n      <td>8804066</td>\n      <td>14</td>\n      <td>11</td>\n      <td>1143</td>\n      <td>2209</td>\n      <td>744</td>\n      <td>0</td>\n      <td>...</td>\n      <td>32</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.000000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>Benign</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 80 columns</p>\n</div>"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "executionInfo": {
     "elapsed": 9,
     "status": "ok",
     "timestamp": 1670604426856,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "mkez4dRDyZ4L",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "label_encoder = LabelEncoder()\n",
    "df = df.replace([np.inf, -np.inf], np.nan)\n",
    "df = df.dropna()\n",
    "df = df.reset_index(drop=True)\n",
    "df = df.drop(['Timestamp'], axis=1)\n",
    "\n",
    "features = len(df.columns) - 1\n",
    "\n",
    "df['Label'] = label_encoder.fit_transform(df['Label'])\n",
    "classes = df['Label'].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 488
    },
    "executionInfo": {
     "elapsed": 10,
     "status": "ok",
     "timestamp": 1670604426857,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "6wz-53mHnm7p",
    "outputId": "fe64ae3c-8710-4fbe-fc93-ff39924faffd",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "         Dst Port  Protocol  Flow Duration  Tot Fwd Pkts  Tot Bwd Pkts  \\\n0               0         0      112641719             3             0   \n1               0         0      112641466             3             0   \n2               0         0      112638623             3             0   \n3              22         6        6453966            15            10   \n4              22         6        8804066            14            11   \n...           ...       ...            ...           ...           ...   \n1044746        80         6       10156986             5             5   \n1044747        80         6            117             2             0   \n1044748        80         6        5095331             3             1   \n1044749        80         6        5235511             3             1   \n1044750       443         6        5807256             6             4   \n\n         TotLen Fwd Pkts  TotLen Bwd Pkts  Fwd Pkt Len Max  Fwd Pkt Len Min  \\\n0                      0                0                0                0   \n1                      0                0                0                0   \n2                      0                0                0                0   \n3                   1239             2273              744                0   \n4                   1143             2209              744                0   \n...                  ...              ...              ...              ...   \n1044746             1089             1923              587                0   \n1044747                0                0                0                0   \n1044748                0                0                0                0   \n1044749                0                0                0                0   \n1044750              327              145              245                0   \n\n         Fwd Pkt Len Mean  ...  Fwd Seg Size Min  Active Mean  Active Std  \\\n0                0.000000  ...                 0          0.0         0.0   \n1                0.000000  ...                 0          0.0         0.0   \n2                0.000000  ...                 0          0.0         0.0   \n3               82.600000  ...                32          0.0         0.0   \n4               81.642857  ...                32          0.0         0.0   \n...                   ...  ...               ...          ...         ...   \n1044746        217.800000  ...                20          0.0         0.0   \n1044747          0.000000  ...                20          0.0         0.0   \n1044748          0.000000  ...                20          0.0         0.0   \n1044749          0.000000  ...                20          0.0         0.0   \n1044750         54.500000  ...                20     291569.0         0.0   \n\n         Active Max  Active Min   Idle Mean    Idle Std  Idle Max  Idle Min  \\\n0                 0           0  56320859.5  139.300036  56320958  56320761   \n1                 0           0  56320733.0  114.551299  56320814  56320652   \n2                 0           0  56319311.5  301.934596  56319525  56319098   \n3                 0           0         0.0    0.000000         0         0   \n4                 0           0         0.0    0.000000         0         0   \n...             ...         ...         ...         ...       ...       ...   \n1044746           0           0         0.0    0.000000         0         0   \n1044747           0           0         0.0    0.000000         0         0   \n1044748           0           0         0.0    0.000000         0         0   \n1044749           0           0         0.0    0.000000         0         0   \n1044750      291569      291569   5515650.0    0.000000   5515650   5515650   \n\n         Label  \n0            0  \n1            0  \n2            0  \n3            0  \n4            0  \n...        ...  \n1044746      0  \n1044747      0  \n1044748      0  \n1044749      0  \n1044750      0  \n\n[1044751 rows x 79 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Dst Port</th>\n      <th>Protocol</th>\n      <th>Flow Duration</th>\n      <th>Tot Fwd Pkts</th>\n      <th>Tot Bwd Pkts</th>\n      <th>TotLen Fwd Pkts</th>\n      <th>TotLen Bwd Pkts</th>\n      <th>Fwd Pkt Len Max</th>\n      <th>Fwd Pkt Len Min</th>\n      <th>Fwd Pkt Len Mean</th>\n      <th>...</th>\n      <th>Fwd Seg Size Min</th>\n      <th>Active Mean</th>\n      <th>Active Std</th>\n      <th>Active Max</th>\n      <th>Active Min</th>\n      <th>Idle Mean</th>\n      <th>Idle Std</th>\n      <th>Idle Max</th>\n      <th>Idle Min</th>\n      <th>Label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>0</td>\n      <td>112641719</td>\n      <td>3</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>56320859.5</td>\n      <td>139.300036</td>\n      <td>56320958</td>\n      <td>56320761</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>0</td>\n      <td>112641466</td>\n      <td>3</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>56320733.0</td>\n      <td>114.551299</td>\n      <td>56320814</td>\n      <td>56320652</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>0</td>\n      <td>112638623</td>\n      <td>3</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>56319311.5</td>\n      <td>301.934596</td>\n      <td>56319525</td>\n      <td>56319098</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>22</td>\n      <td>6</td>\n      <td>6453966</td>\n      <td>15</td>\n      <td>10</td>\n      <td>1239</td>\n      <td>2273</td>\n      <td>744</td>\n      <td>0</td>\n      <td>82.600000</td>\n      <td>...</td>\n      <td>32</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.000000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>22</td>\n      <td>6</td>\n      <td>8804066</td>\n      <td>14</td>\n      <td>11</td>\n      <td>1143</td>\n      <td>2209</td>\n      <td>744</td>\n      <td>0</td>\n      <td>81.642857</td>\n      <td>...</td>\n      <td>32</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.000000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>1044746</th>\n      <td>80</td>\n      <td>6</td>\n      <td>10156986</td>\n      <td>5</td>\n      <td>5</td>\n      <td>1089</td>\n      <td>1923</td>\n      <td>587</td>\n      <td>0</td>\n      <td>217.800000</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.000000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1044747</th>\n      <td>80</td>\n      <td>6</td>\n      <td>117</td>\n      <td>2</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.000000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1044748</th>\n      <td>80</td>\n      <td>6</td>\n      <td>5095331</td>\n      <td>3</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.000000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1044749</th>\n      <td>80</td>\n      <td>6</td>\n      <td>5235511</td>\n      <td>3</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.000000</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0.000000</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1044750</th>\n      <td>443</td>\n      <td>6</td>\n      <td>5807256</td>\n      <td>6</td>\n      <td>4</td>\n      <td>327</td>\n      <td>145</td>\n      <td>245</td>\n      <td>0</td>\n      <td>54.500000</td>\n      <td>...</td>\n      <td>20</td>\n      <td>291569.0</td>\n      <td>0.0</td>\n      <td>291569</td>\n      <td>291569</td>\n      <td>5515650.0</td>\n      <td>0.000000</td>\n      <td>5515650</td>\n      <td>5515650</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>1044751 rows × 79 columns</p>\n</div>"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 331
    },
    "executionInfo": {
     "elapsed": 9,
     "status": "ok",
     "timestamp": 1670604426857,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "pDdBjmOgVZdA",
    "outputId": "649a90de-aad7-4207-a533-07f3a793c1d5",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "       Dst Port  Protocol  Flow Duration  Tot Fwd Pkts  Tot Bwd Pkts  \\\nLabel                                                                  \n0        663808    663808         663808        663808        663808   \n1        193354    193354         193354        193354        193354   \n2        187589    187589         187589        187589        187589   \n\n       TotLen Fwd Pkts  TotLen Bwd Pkts  Fwd Pkt Len Max  Fwd Pkt Len Min  \\\nLabel                                                                       \n0               663808           663808           663808           663808   \n1               193354           193354           193354           193354   \n2               187589           187589           187589           187589   \n\n       Fwd Pkt Len Mean  ...  Fwd Act Data Pkts  Fwd Seg Size Min  \\\nLabel                    ...                                        \n0                663808  ...             663808            663808   \n1                193354  ...             193354            193354   \n2                187589  ...             187589            187589   \n\n       Active Mean  Active Std  Active Max  Active Min  Idle Mean  Idle Std  \\\nLabel                                                                         \n0           663808      663808      663808      663808     663808    663808   \n1           193354      193354      193354      193354     193354    193354   \n2           187589      187589      187589      187589     187589    187589   \n\n       Idle Max  Idle Min  \nLabel                      \n0        663808    663808  \n1        193354    193354  \n2        187589    187589  \n\n[3 rows x 78 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Dst Port</th>\n      <th>Protocol</th>\n      <th>Flow Duration</th>\n      <th>Tot Fwd Pkts</th>\n      <th>Tot Bwd Pkts</th>\n      <th>TotLen Fwd Pkts</th>\n      <th>TotLen Bwd Pkts</th>\n      <th>Fwd Pkt Len Max</th>\n      <th>Fwd Pkt Len Min</th>\n      <th>Fwd Pkt Len Mean</th>\n      <th>...</th>\n      <th>Fwd Act Data Pkts</th>\n      <th>Fwd Seg Size Min</th>\n      <th>Active Mean</th>\n      <th>Active Std</th>\n      <th>Active Max</th>\n      <th>Active Min</th>\n      <th>Idle Mean</th>\n      <th>Idle Std</th>\n      <th>Idle Max</th>\n      <th>Idle Min</th>\n    </tr>\n    <tr>\n      <th>Label</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>...</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n      <td>663808</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>...</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n      <td>193354</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>...</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n      <td>187589</td>\n    </tr>\n  </tbody>\n</table>\n<p>3 rows × 78 columns</p>\n</div>"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('Label').count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "executionInfo": {
     "elapsed": 4,
     "status": "ok",
     "timestamp": 1670604427264,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "9s_HaYjkzuKk",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "y = df.pop('Label')\n",
    "X = df\n",
    "\n",
    "normalizer_scaler = preprocessing.Normalizer()\n",
    "x_scaled = normalizer_scaler.fit_transform(X)\n",
    "X = pd.DataFrame(x_scaled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "executionInfo": {
     "elapsed": 4,
     "status": "ok",
     "timestamp": 1670604427264,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "l-9LdOome2ck",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "executionInfo": {
     "elapsed": 3,
     "status": "ok",
     "timestamp": 1670604427264,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "FC6lXk4Az3yB",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "X_train = tf.convert_to_tensor(X_train)\n",
    "y_train = tf.convert_to_tensor(y_train)\n",
    "\n",
    "X_test = tf.convert_to_tensor(X_test)\n",
    "y_test = tf.convert_to_tensor(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "executionInfo": {
     "elapsed": 333,
     "status": "ok",
     "timestamp": 1670604427594,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "SP8ckOayytne",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "input = tf.keras.layers.Input((features,), name='feature')\n",
    "\n",
    "n1 = tf.keras.layers.Dense(16)(input)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(32)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(64)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(128)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(256)(n1)\n",
    "\n",
    "n1 = tf.keras.layers.MultiHeadAttention(num_heads=int(256/8), key_dim=256, value_dim=256, attention_axes=1)(n1, n1, n1)\n",
    "\n",
    "n1 = tf.keras.layers.Dense(256)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(128)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(64)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(32)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(16)(n1)\n",
    "\n",
    "output = tf.keras.layers.Dense(classes, activation='softmax')(n1)\n",
    "\n",
    "model = tf.keras.Model(inputs=input, outputs=output)\n",
    "\n",
    "adv_config = nsl.configs.make_adv_reg_config(multiplier=0.2, adv_step_size=0.05)\n",
    "adv_model = nsl.keras.AdversarialRegularization(model, adv_config=adv_config)\n",
    "\n",
    "# Compile, train, and evaluate.\n",
    "adv_model.compile(optimizer='adam',\n",
    "                  loss='sparse_categorical_crossentropy',\n",
    "                  metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 269,
     "status": "ok",
     "timestamp": 1670604427855,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "-EUxz4vRy7YP",
    "outputId": "1c8c1a81-6e6b-4129-f8a2-9d22f904cb87",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"model_2\"\n",
      "__________________________________________________________________________________________________\n",
      " Layer (type)                   Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      " feature (InputLayer)           [(None, 78)]         0           []                               \n",
      "                                                                                                  \n",
      " dense_22 (Dense)               (None, 16)           1264        ['feature[0][0]']                \n",
      "                                                                                                  \n",
      " dropout_16 (Dropout)           (None, 16)           0           ['dense_22[0][0]']               \n",
      "                                                                                                  \n",
      " dense_23 (Dense)               (None, 32)           544         ['dropout_16[0][0]']             \n",
      "                                                                                                  \n",
      " dropout_17 (Dropout)           (None, 32)           0           ['dense_23[0][0]']               \n",
      "                                                                                                  \n",
      " dense_24 (Dense)               (None, 64)           2112        ['dropout_17[0][0]']             \n",
      "                                                                                                  \n",
      " dropout_18 (Dropout)           (None, 64)           0           ['dense_24[0][0]']               \n",
      "                                                                                                  \n",
      " dense_25 (Dense)               (None, 128)          8320        ['dropout_18[0][0]']             \n",
      "                                                                                                  \n",
      " dropout_19 (Dropout)           (None, 128)          0           ['dense_25[0][0]']               \n",
      "                                                                                                  \n",
      " dense_26 (Dense)               (None, 256)          33024       ['dropout_19[0][0]']             \n",
      "                                                                                                  \n",
      " multi_head_attention_2 (MultiH  (None, 256)         8413440     ['dense_26[0][0]',               \n",
      " eadAttention)                                                    'dense_26[0][0]',               \n",
      "                                                                  'dense_26[0][0]']               \n",
      "                                                                                                  \n",
      " dense_27 (Dense)               (None, 256)          65792       ['multi_head_attention_2[0][0]'] \n",
      "                                                                                                  \n",
      " dropout_20 (Dropout)           (None, 256)          0           ['dense_27[0][0]']               \n",
      "                                                                                                  \n",
      " dense_28 (Dense)               (None, 128)          32896       ['dropout_20[0][0]']             \n",
      "                                                                                                  \n",
      " dropout_21 (Dropout)           (None, 128)          0           ['dense_28[0][0]']               \n",
      "                                                                                                  \n",
      " dense_29 (Dense)               (None, 64)           8256        ['dropout_21[0][0]']             \n",
      "                                                                                                  \n",
      " dropout_22 (Dropout)           (None, 64)           0           ['dense_29[0][0]']               \n",
      "                                                                                                  \n",
      " dense_30 (Dense)               (None, 32)           2080        ['dropout_22[0][0]']             \n",
      "                                                                                                  \n",
      " dropout_23 (Dropout)           (None, 32)           0           ['dense_30[0][0]']               \n",
      "                                                                                                  \n",
      " dense_31 (Dense)               (None, 16)           528         ['dropout_23[0][0]']             \n",
      "                                                                                                  \n",
      " dense_32 (Dense)               (None, 3)            51          ['dense_31[0][0]']               \n",
      "                                                                                                  \n",
      "==================================================================================================\n",
      "Total params: 8,568,307\n",
      "Trainable params: 8,568,307\n",
      "Non-trainable params: 0\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 48
    },
    "executionInfo": {
     "elapsed": 447,
     "status": "ok",
     "timestamp": 1670604428298,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "844W7YPn82Gw",
    "outputId": "ea88bcec-2ee8-4229-e43f-6c1a767fdc89",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You must install pydot (`pip install pydot`) and install graphviz (see instructions at https://graphviz.gitlab.io/download/) for plot_model to work.\n"
     ]
    }
   ],
   "source": [
    "tf.keras.utils.plot_model(model, \n",
    "                          show_shapes=True,\n",
    "                          show_dtype=True,\n",
    "                          show_layer_names=True,\n",
    "                          rankdir='LR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "RjM9Xf2Ry-lq",
    "outputId": "3f4f80c6-a5ac-4e5e-8981-be5081cd7e3c",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "1633/1633 [==============================] - 584s 356ms/step - loss: 0.3274 - sparse_categorical_crossentropy: 0.2217 - sparse_categorical_accuracy: 0.8976 - scaled_adversarial_loss: 0.1057 - val_loss: 0.2821 - val_sparse_categorical_crossentropy: 0.1604 - val_sparse_categorical_accuracy: 0.9963 - val_scaled_adversarial_loss: 0.1217\n",
      "Epoch 2/10\n",
      "1633/1633 [==============================] - 558s 342ms/step - loss: 0.1510 - sparse_categorical_crossentropy: 0.0769 - sparse_categorical_accuracy: 0.9724 - scaled_adversarial_loss: 0.0741 - val_loss: 0.0542 - val_sparse_categorical_crossentropy: 0.0133 - val_sparse_categorical_accuracy: 0.9972 - val_scaled_adversarial_loss: 0.0409\n",
      "Epoch 3/10\n",
      "1633/1633 [==============================] - 558s 342ms/step - loss: 0.1322 - sparse_categorical_crossentropy: 0.0680 - sparse_categorical_accuracy: 0.9756 - scaled_adversarial_loss: 0.0642 - val_loss: 0.4386 - val_sparse_categorical_crossentropy: 0.3888 - val_sparse_categorical_accuracy: 0.9081 - val_scaled_adversarial_loss: 0.0498\n",
      "Epoch 4/10\n",
      "1633/1633 [==============================] - 553s 339ms/step - loss: 0.1225 - sparse_categorical_crossentropy: 0.0599 - sparse_categorical_accuracy: 0.9785 - scaled_adversarial_loss: 0.0626 - val_loss: 0.0342 - val_sparse_categorical_crossentropy: 0.0116 - val_sparse_categorical_accuracy: 0.9971 - val_scaled_adversarial_loss: 0.0226\n",
      "Epoch 5/10\n",
      "1633/1633 [==============================] - 557s 341ms/step - loss: 0.0959 - sparse_categorical_crossentropy: 0.0386 - sparse_categorical_accuracy: 0.9894 - scaled_adversarial_loss: 0.0572 - val_loss: 0.0668 - val_sparse_categorical_crossentropy: 0.0199 - val_sparse_categorical_accuracy: 0.9962 - val_scaled_adversarial_loss: 0.0469\n",
      "Epoch 6/10\n",
      "1633/1633 [==============================] - 582s 357ms/step - loss: 0.1351 - sparse_categorical_crossentropy: 0.0675 - sparse_categorical_accuracy: 0.9803 - scaled_adversarial_loss: 0.0675 - val_loss: 0.0464 - val_sparse_categorical_crossentropy: 0.0133 - val_sparse_categorical_accuracy: 0.9970 - val_scaled_adversarial_loss: 0.0331\n",
      "Epoch 7/10\n",
      "1633/1633 [==============================] - 583s 357ms/step - loss: 0.0913 - sparse_categorical_crossentropy: 0.0342 - sparse_categorical_accuracy: 0.9910 - scaled_adversarial_loss: 0.0571 - val_loss: 0.0558 - val_sparse_categorical_crossentropy: 0.0153 - val_sparse_categorical_accuracy: 0.9969 - val_scaled_adversarial_loss: 0.0404\n",
      "Epoch 8/10\n",
      "1633/1633 [==============================] - 582s 356ms/step - loss: 0.1116 - sparse_categorical_crossentropy: 0.0534 - sparse_categorical_accuracy: 0.9818 - scaled_adversarial_loss: 0.0582 - val_loss: 0.0587 - val_sparse_categorical_crossentropy: 0.0105 - val_sparse_categorical_accuracy: 0.9980 - val_scaled_adversarial_loss: 0.0482\n",
      "Epoch 9/10\n",
      "1633/1633 [==============================] - 563s 345ms/step - loss: 0.1159 - sparse_categorical_crossentropy: 0.0545 - sparse_categorical_accuracy: 0.9823 - scaled_adversarial_loss: 0.0614 - val_loss: 0.0352 - val_sparse_categorical_crossentropy: 0.0119 - val_sparse_categorical_accuracy: 0.9975 - val_scaled_adversarial_loss: 0.0233\n",
      "Epoch 10/10\n",
      "1633/1633 [==============================] - 562s 344ms/step - loss: 0.1062 - sparse_categorical_crossentropy: 0.0431 - sparse_categorical_accuracy: 0.9886 - scaled_adversarial_loss: 0.0631 - val_loss: 0.0719 - val_sparse_categorical_crossentropy: 0.0152 - val_sparse_categorical_accuracy: 0.9976 - val_scaled_adversarial_loss: 0.0567\n"
     ]
    },
    {
     "data": {
      "text/plain": "<keras.callbacks.History at 0x21d6083b970>"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adv_model.fit({'feature': X_train, 'label': y_train}, epochs=10, batch_size=512, validation_data={'feature': X_test, 'label': y_test})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "id": "Pmt7mlJoQ-Np",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "208951/208951 [==============================] - 1419s 7ms/step - loss: 0.0719 - sparse_categorical_crossentropy: 0.0152 - sparse_categorical_accuracy: 0.9976 - scaled_adversarial_loss: 0.0567\n"
     ]
    },
    {
     "data": {
      "text/plain": "[0.0719413161277771,\n 0.015213510021567345,\n 0.9975687861442566,\n 0.056713417172431946]"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adv_model.evaluate({'feature':X_test, 'label':y_test}, batch_size=1)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": [],
   "mount_file_id": "1l_2YpamsRqa2kvQbF6VcRh1L-ZVNw2LN",
   "authorship_tag": "ABX9TyOZ6tqQxYPy3g7WnP4d6Wrs"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}